package com.labor_management_system.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.labor_management_system.Entity.*;
import com.labor_management_system.Mapper.WorkDetailsMapper;
import com.labor_management_system.Service.*;
import com.labor_management_system.Utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

public class ProjectStatisticsController {
    //项目统计管理


    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectListService projectListService;
    @Autowired
    private LaborEngineeringService laborEngineeringService;
    @Autowired
    private WorkDetailsService workDetailsService;
    @Autowired
    private TeamService teamService;


    //新建项目
    //前端传来了项目信息，项目清单
    @PostMapping("AddProject")
    public R AddProject(@RequestParam("Project") String projectJson,
                        @RequestParam("ProjectList") String projectListJson
    ) throws JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();

        Project project = objectMapper.readValue(projectJson,Project.class);

        System.out.println("project = " + project);

        List<ProjectList> projectLists = objectMapper.readValue(projectListJson, new TypeReference<List<ProjectList>>() {});

        System.out.println("projectLists = " + projectLists);

        //projectId先放入每个实体类中
        String Id = UUID.randomUUID().toString().replace("-", "");

        project.setProjectId(Id);

        for (int i = 0; i < projectLists.size(); i++) {
            projectLists.get(i).setProjectId(Id);
        }

        return projectService.insertProject(project) &&
                projectListService.inserBatchList(projectLists)? R.ok():R.error().message("新增失败");

    }

    //显示全部项目，分页查询
    @PostMapping("ShowAllProject")
    public R ShowAllProject(@RequestParam(value = "selectPage",defaultValue = "1") Integer selectPage,@RequestParam(value = "PageSize",defaultValue = "100") Integer PageSize){
        return R.ok().data("ProjectInfoList",projectService.showAllProject(selectPage,PageSize));
    }

    //条件查询，项目名称(projectName)，项目阶段(projectPhase)，业主名称(owner)，是否派工(dispatchWorkers)
    @PostMapping("SelectByProject")
    public R SelectByProject(@RequestBody Project project){
        return R.ok().data("ProjectInfoList",projectService.selectBy(project));
    }

    //点击项目名称，查看该项目具体信息
    //传projectId和teamId
    @PostMapping("detailOfproject")
    public R detailOfproject(@RequestParam("projectId") String projectId,@RequestParam("teamId") String teamId){

        ProjectDTO projectDTO = new ProjectDTO();

        projectDTO.setProject(projectService.selectProjectById(projectId));

        projectDTO.setProjectListList(projectListService.selectProjectListById(projectId));

        projectDTO.setTeam(teamService.slectTeamById(teamId));

        return R.ok().data("detail",projectDTO);
    }


    //班组信息选择器
    @PostMapping("teamselect")
    public R teamselect(){
        return R.ok().data("teamList",teamService.selectAllTeam());
    }

    //编辑项目
    @PostMapping("EditProject")
    public R EditProject(@RequestParam("Project") String projectJson,
                         @RequestParam("ProjectList") String projectListJson
    ) throws JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();

        Project project = objectMapper.readValue(projectJson,Project.class);

        System.out.println("project = " + project);

        List<ProjectList> projectLists = objectMapper.readValue(projectListJson, new TypeReference<List<ProjectList>>() {});

        System.out.println("projectLists = " + projectLists);

        String projectId = project.getProjectId();

        return projectService.updateProject(project,projectId)
                && projectListService.updateBatchList(projectLists,projectId)?
                R.ok():R.error().message("新增失败");
    }

    //删除项目
    //根据projectId删除，注意还要删除连带的项目清单
    //还要删除劳务工程
    @PostMapping("DeleteProject")
    public R DeleteProject(@RequestParam("projectId") String projectId){
        //TODO:
        //查询主表的数据
        Project project = projectService.getBaseMapper().selectById(projectId);

        //如果主表数据存在
        if(project !=null){
            //查询关联表的外键数据
            List<LaborEngineering> laborEngineeringList = laborEngineeringService.getBaseMapper().selectList(new QueryWrapper<LaborEngineering>().eq("project_id", projectId));

            //逐一处理关联表的数据
            for(LaborEngineering laborEngineering:laborEngineeringList){
                List<WorkDetails> workDetailsList = workDetailsService.getBaseMapper().selectList(new QueryWrapper<WorkDetails>().eq("labor_engineering_id", laborEngineering.getLaborEngineeringId()));

                //逐一删除第三张表的的外键数据
                for(WorkDetails workDetails:workDetailsList){
                    workDetailsService.getBaseMapper().deleteById(workDetails.getWorkDetailsId());
                }

                // 最后删除关联表的数据
                laborEngineeringService.getBaseMapper().deleteById(laborEngineering.getLaborEngineeringId());
            }

            //删除子表
            QueryWrapper<ProjectList> listQueryWrapper = new QueryWrapper<>();
            listQueryWrapper.eq("project_id",projectId);
            projectListService.getBaseMapper().delete(listQueryWrapper);

            // 最后删除主表的数据
            projectService.getBaseMapper().deleteById(projectId);
        }





        return R.ok();
    }


    //删除项目清单
    //根据projectListId删除
    @PostMapping("DeleteProjectList")
    public R DeleteProjectList(@RequestParam("projectListId") String projectListId){

        projectListService.deleteprojectlist(projectListId);

        return R.ok();
    }
}
